<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="page" uri="http://www.yf1701.com/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base target="${cxt }/">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户地址</title>


		<link href="css/personalcenter/address.css" rel="stylesheet" type="text/css"/>
		<script src="js/index/jquery-2.1.3.min.js"></script>
		<script src="js/index/citys.js"></script>
		<script src="js/index/sf.js"></script>
		<script type="text/javascript">
		
		
		$(function(){
			//初始化省份信息
		for(var i=0;i<sf.length;i++){
			var op=$("<option>");
			op.val(sf[i].ProID);
			op.text(sf[i].name);
			//给省份下拉列表添加子元素
			$("#sf").append(op);
		}
			//给省份下拉列表添加绑定事件
		$("#sf").change(function(){
		//获取省份的编号
		var pid = $(this).val();
		//清空城市列表
		document.getElementById("city").options.length=0;
		 //遍历城市JSON找出城市对应的省份
		for(var i=0;i<cities.length;i++){
			if(cities[i].ProID==pid){
				//创建城市选项
				var cop=$("<option>");
				cop.val(cities[i].CityID);
				cop.text(cities[i].name);
				//给城市列表添加子元素
				$("#city").append(cop);
						
					}
			
				}
		});
	});
		

		
		function YZmessage(){

			
			var linkman = $("input[name=linkman]").val();
			var han = /^[\u4e00-\u9fa5]+$/;
			if (linkman == null
					|| linkman== "") {
				alert("联系人不能为空");  //修改错误提示信息
				$("input[name=linkman]").focus();
				 return false;
			}
		
				 if (!han.test(linkman)) {
				  alert("姓名格式不对");
				  $("input[name=linkman]").focus();
				  return false;
				  };
		
				
	}
			
	
		function YZidCard(){
			var idCard = $("input[name=idCard]").val();
			 //15位和18位身份证号码的正则表达式  
		    var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;  
		    //如果通过该验证，说明身份证格式正确，但准确性还需计算  
		    if (regIdCard.test(idCard)) {  
		        if (idCard.length == 18) {  
		            var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里  
		            var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组  
		            var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和  
		            for (var i = 0; i < 17; i++) {  
		                idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];  
		            }  
		            var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置  
		            var idCardLast = idCard.substring(17);//得到最后一位身份证号码  
		            //如果等于2，则说明校验码是10，身份证号码最后一位应该是X  
		            if (idCardMod == 2) {  
		                if (idCardLast == "X" || idCardLast == "x") {  
		                    return true;  
		                   // alert("恭喜通过验证啦！");  
		                } else {  
		                    
		                    alert("身份证号码错误！"); 
		                    $("input[name=idCard]").focus();
		                    return false;  
		                }  
		            } else {  
		                //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码  
		                if (idCardLast == idCardY[idCardMod]) {  
		                    //alert("恭喜通过验证啦！");  
		                    return true;  
		                } else {  
		                  
		                    alert("身份证号码错误！"); 
		                    $("input[name=idCard]").focus();
		                    return false;  
		                }  
		            }  
		        }  
		    } else {  
		        alert("身份证格式不正确!");
		        $("input[name=idCard]").focus();
		        return false;  
		    }  
			
			
		}
	
			function YZaddress(){
				
				var address =$("input[name=address]").val();
			
				
				var veg=/^(((13[0-9]{1})|159|158|157|156|155|153|150|151|159|182|185|187|188|189|186|145|176)+\d{8})$/;
				if (address == null
						|| address== "") {
					alert("手机号码不能为空");  //修改错误提示信息
					 return false;
				}
			
				if(!veg.test(address)){
					alert("手机格式错误");  
					$("input[name=address]").focus();
					 return false;
				}
					
				if($('#sf option:selected').text()==null || $('#city option:selected').text()==null){
					alert("请选择对应的省份和城市");
				}
				
				
				
				
				 $.ajax({
						url:"adl",
						type:"get",
						data:{linkman:$("input[name='linkman']").val(),idCard:$("input[name='idCard']").val(),address:$("input[name='address']").val(),province:$('#sf option:selected').text(),city:$('#city option:selected').text(),specific:$("#specific").val()},
						datatype:"json",
						success:function(e){
						alert(JSON.stringify(e));	
						location.href="pmain";
							
				}
				 
					
			});
			
}	
			
			
		$(function(){
			
			$("[useraddress_id]").click(function(){
				
				var msg = "您真的确定要删除吗？\n\n请确认！"; 
				 if (confirm(msg)==true){
	
				var ua = $(this);
				var uaddress_id = $(this).attr("useraddress_id");
				//alert("dizhi id :"+uaddress_id);
				$.get("delete",{id:uaddress_id},function(data,success){
					// 先清除第一行后面的内容
					if(data=="删除成功！"){
						alert(data);
						console.log($(this));
						ua.parent().parent().remove();
					}
					
				},"text")
			}else{
				return false;
			}
		})
			
			
})
			

			

		
		
		</script>
		
</head>
	<body>
		<div class="wrap_right">
			<div class="wrap_right1"><span>新增租车人信息:</span>
		
			</div>
			
				<div class="wrap_right2">
				<ul>
					<li>
						<span>联系人：</span>
						<div class="wrap_right2_div">
							<input name="linkman" class="wrap_right2_input"/>
						</div>
					</li>
					
					
					<li>
						<span style="margin-left: 80px;">身份证号码：</span>
						<div class="wrap_right2_div">
								<input class="wrap_right2_input" name="idCard" onclick="YZmessage()"  />
						</div>
					</li>
					
					
					<li>
						<span style="margin-left: 105px;">联系号码：</span>
						<div class="wrap_right2_div">
								<input class="wrap_right2_input" name="address" onclick="YZidCard()"/>
						</div>
					</li>
					<li> 
					<span style="margin-left:105px;">联系地址:</span>
					<div class="wrap_right2_div">
					<select id="sf" style="height:25px;"></select>
					<select id="city" style="height:25px;"></select>
					<input style="width:250px;height:25px;" id="specific" placeholder="这里输入详细地址" />
					</div>
					</li>
					
					<li><input type="button" value="保存" class="wrap_right2_button" onclick="YZaddress()"/></li>
				</ul>
			</div>
		
		<div class="wrap_right3">
				<div class="wrap_right3_div">
					<span>地址</span>
				</div>
				<table>
					<tr style="height: 34px;font-size: 15px;">
						<th>联系人</th>
						<th>身份证号码</th>
						<th>联系号码</th>
						<th>地址</th>
						<th>操作</th>
					</tr>
					
		<c:forEach items="${useraddressList.data}" var="ua">
            <tr class="div_tr">
                <td>${ua.name}</td>
                <td>${ua.idcard}</td>
                <td>${ua.tphone}</td>
                 <td>${ua.address}</td>
                <td><a useraddress_id="${ua.id }" name="delete">删除</a></td>
            </tr>
        </c:forEach>

				</table>
				
				<div class="div_tr_div">
					<page:page pages="${useraddressList.pages }" 
						total="${useraddressList.total }"
						url="pmain"
						currpage="${useraddressList.currpage }"
						pagesize="${useraddressList.pagesize }" />
					
				</div>
				
				
				
			</div>		
		</div>
		
	</body>
</html>
